{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "43c16616-dc55-45a8-8fc3-51db64460aea",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#字典创建方式\n",
    "d1 = {'name':'jason','age':20,'gender':'male'}\n",
    "d2 = dict({'name':'jason','age':20,'gender':'male'})\n",
    "d3 = dict([('name','jason'),('age',20),('gender','male')])\n",
    "d4 = dict(name='jason',age=20,gender='male')\n",
    "d1==d2==d3==d4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "a29a20ff-f5ac-4110-b26d-207d702329ea",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 集合创建方式\n",
    "s1 = {1,2,3,4} \n",
    "s2 = set([1,2,3,4]) \n",
    "s1 == s2 \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "52eef48a-8f87-4d1d-91b9-49d0250e790b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s = {1,'hello',5.0}\n",
    "# s[0] 集合不支持索引查找元素\n",
    "1 in s\n",
    "'name' in d1\n",
    "'location' in d1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "57b1800e-ed59-4db3-b631-74da514d5954",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dict_items([('name', 'jason'), ('age', 20), ('gender', 'male')])\n"
     ]
    }
   ],
   "source": [
    "#集合和字典支持增加、删除 更新操作 \n",
    "d = {'name':'jason','age':20} \n",
    "d['gender'] = 'male' \n",
    "d['dod'] = '1999-01-10' \n",
    "d\n",
    "d['dod'] = '2000-01-10' \n",
    "d \n",
    "d.pop('dod')\n",
    "d\n",
    "s = {1,2,3} \n",
    "s.add(4) \n",
    "s\n",
    "s.remove(4)  \n",
    "print(d.items())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "5829ed85-87af-4cf7-973c-07359b36fa21",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[('b', 1), ('a', 2), ('c', 10)]\n",
      "[('a', 2), ('b', 1), ('c', 10)]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[('b', 1), ('a', 2), ('c', 10)]"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 排序 \n",
    "d = {'b':1,'a':2,'c':10} \n",
    "d_sorted_by_key = sorted(d.items(),key=lambda x:x[0]) # 根据字典健升级 \n",
    "d_sored_by_value = sorted(d.items(),key = lambda x:x[1]) # 根据字典值进行升序排序\n",
    "d_sored_by_value\n",
    "#d_sorted_by_key = dict(d_sorted_by_key)\n",
    "d_sorted_by_key\n",
    "print(d_sored_by_value)\n",
    "print(d_sorted_by_key)\n",
    "# 集合排序 \n",
    "s = {4,3,2,1} \n",
    "sorted(s) \n",
    "d_sored_by_value"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "0bfde197-9637-43b7-b793-88695714ca53",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{30, 100, 150}"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 集合Set应用情景\n",
    "products = [ (143121312, 100), (432314553, 30), (32421912367, 150), (937153201, 30)]\n",
    "unique_price_set = set() \n",
    "for _,price in products:\n",
    "    unique_price_set.add(price)\n",
    "unique_price_set"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "ff8447dc-8fae-4bc4-bc14-d3c1312f05be",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'Red': 1, 'Yellow': 1, 'Purple': 1, 'Blue': 3}\n"
     ]
    }
   ],
   "source": [
    "word_list = ['Red','Yellow','Purple','Blue','Blue','Blue']\n",
    "word_cnt = {}\n",
    "for word in word_list:\n",
    "    word_cnt[word] = word_cnt.get(word,0) +1\n",
    "print(word_cnt)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "289c52c2-2f6e-4bca-bc66-8d91be29b79d",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
